package com.hyq.market.ui.home.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hyq.market.R;
import com.hyq.market.adapter.HotGoodsAdapter;
import com.hyq.market.adapter.IndexClassAdapter;
import com.hyq.market.base.AbsFragment;
import com.hyq.market.widget.CustomLoadMoreView;
import com.hyq.market.widget.ShowMsgUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGABanner;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by Administrator on 2017/7/19 0019.
 */

public class HomeFt extends AbsFragment implements IViewHome {
    @BindView(R.id.banner_guide_content)
    BGABanner bannerGuideContent;
    Unbinder unbinder;
    View rootView;
    @BindView(R.id.recycler_hot_class)
    RecyclerView recyclerHotClass;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.recycler_hot_goods)
    RecyclerView recyclerHotGoods;
    @BindView(R.id.fragment_rotate_header_with_text_view_frame)
    PtrClassicFrameLayout hFrame;
    private IndexClassAdapter indexClassAdapter;
    private HotGoodsAdapter hotGoodsAdapter;
    PersenterHome presenter;
    private CustomLoadMoreView customLoadMoreView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_layout_home, null);
        unbinder = ButterKnife.bind(this, rootView);
        bannerGuideContent.setData(R.mipmap.gui2, R.mipmap.gui3, R.mipmap.gui2, R.mipmap.gui3);
        presenter = new PersenterHome(this);
        initView();
        presenter.getData(null);
        return rootView;
    }

    @Override
    public MvpPresenter createPresenter() {
        return presenter;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    void initView() {
        hFrame.setLastUpdateTimeRelateObject(this);
        hFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.onRefresh();
            }
        });
        indexClassAdapter = new IndexClassAdapter();
        // indexClassAdapter.setOnItemChildClickListener(presenter);
        indexClassAdapter.setOnItemClickListener(presenter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 5);
        recyclerHotClass.setLayoutManager(gridLayoutManager);
        recyclerHotClass.setAdapter(indexClassAdapter);
        recyclerHotClass.setNestedScrollingEnabled(false);


        hotGoodsAdapter = new HotGoodsAdapter();
        hotGoodsAdapter.setOnItemChildClickListener(presenter);
        hotGoodsAdapter.setOnItemClickListener(presenter);
        hotGoodsAdapter.setOnLoadMoreListener(presenter, recyclerHotGoods);
        customLoadMoreView = new CustomLoadMoreView();
        hotGoodsAdapter.setLoadMoreView(customLoadMoreView);
        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(getContext(), 2);
        recyclerHotGoods.setLayoutManager(gridLayoutManager2);
        recyclerHotGoods.setAdapter(hotGoodsAdapter);
        recyclerHotGoods.setNestedScrollingEnabled(false);
    }

    @Override
    protected void lazyLoad() {
        // presenter.loadData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public <T> void loadData(T t) {

    }

    @Override
    public BaseQuickAdapter getAdapter(int tyepe) {
        switch (tyepe) {
            case 1:
                return indexClassAdapter;
            case 2:

                return hotGoodsAdapter;
            default:
                return indexClassAdapter;
        }
    }

    @Override
    public void pullMoreComlite(String... msg) {
        hotGoodsAdapter.loadMoreComplete();
    }

    @Override
    public void pullMoreNoMore(String... msg) {
        hotGoodsAdapter.loadMoreComplete();
        customLoadMoreView.setLoadMoreStatus(CustomLoadMoreView.STATUS_END);
        ShowMsgUtil.hideLoadingDialog();
    }

    @Override
    public void refreshNoData(String... msg) {
       // textNoData.setVisibility(View.VISIBLE);
        ShowMsgUtil.hideLoadingDialog();
    }

    @Override
    public void refreshComplite(String... msg) {
        hFrame.refreshComplete();
        customLoadMoreView.setLoadMoreStatus(CustomLoadMoreView.STATUS_DEFAULT);
        //textNoData.setVisibility(View.GONE);
    }
}
